//
//  GongGaoVC.m
//  智慧小区
//
//  Created by 高永杨 on 2017/3/17.
//  Copyright © 2017年 zhongchuang. All rights reserved.
//

#import "GongGaoVC.h"
#import "GongGaoCell.h"
#import "GongGaoModel.h"
#import "GongGaoDetailVC.h"
#import <MJRefresh.h>

@interface GongGaoVC ()<UITableViewDelegate,UITableViewDataSource>
/**<#参数意思#>*/
@property(nonatomic,strong)NSMutableArray *dataArr;
@property (weak, nonatomic) IBOutlet UITableView *tableVie;
//数据加载的原因
/**当前页码*/
@property (nonatomic,assign) NSInteger page;

/**上一次的请求参数*/
@property (nonatomic,strong) NSDictionary *params;
//
/**数据*/
@property(nonatomic,strong)NSMutableDictionary *dictArr;

/**请求后得到的数据*/
@property(nonatomic,strong)NSMutableArray *responseArr;

@end
static NSString *cellId = @"cellId";
@implementation GongGaoVC
-(void)goBack
{
    [self.navigationController popViewControllerAnimated:YES];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = @"公告消息";
    [self setNav:self];
    NSMutableDictionary *attrs = [NSMutableDictionary dictionary];
    attrs[NSFontAttributeName] = [UIFont systemFontOfSize:20];
    attrs[NSForegroundColorAttributeName] = [UIColor whiteColor];
    self.navigationController.navigationBar.titleTextAttributes = attrs;
    self.tabBarController.tabBar.hidden = YES;
//    [self loadNewData];
    [self setuprefresh];
    [self.tableVie registerNib:[UINib nibWithNibName:NSStringFromClass([GongGaoCell class]) bundle:nil] forCellReuseIdentifier:cellId];
    // Do any additional setup after loading the view from its nib.
//    self.tableVie.estimatedRowHeight = 100;
//    self.tableVie.rowHeight = UITableViewAutomaticDimension;
}

-(void)setuprefresh
{
    self.tableVie.mj_header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(loadNewTopics)];
//    self.tableVie.mj_header.automaticallyChangeAlpha = YES;
    [self.tableVie.mj_header beginRefreshing];
    self.tableVie.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(loadMoreTopic)];
//    self.tableVie.mj_footer.ignoredScrollViewContentInsetBottom = 60;

}
//#pragma mark - 数据处理
-(void)loadNewTopics
{
    [self.tableVie.mj_footer endRefreshing];
    //发送请求
    NSMutableDictionary *parms = [NSMutableDictionary dictionary];
//    parms[@"resultSize"] = @(5);
    parms[@"opt"] = @(1);
    parms[@"page"] = @(1);
    self.params = parms;
    [[AFHTTPSessionManager manager] GET:GolobaUrl parameters:parms progress:^(NSProgress * _Nonnull downloadProgress) {
        
    } success:^(NSURLSessionDataTask * _Nonnull task, NSDictionary* _Nullable responseObject) {
        [SVProgressHUD showSuccessWithStatus:@"刷新成功"];
        [SVProgressHUD setDefaultStyle:SVProgressHUDStyleDark];
        [self.dataArr removeAllObjects];
        NSArray *arr = responseObject[@"results"];
        for (int i = 0; i<arr.count; i++) {
            GongGaoModel *gongGao = [[GongGaoModel alloc] init];
            [gongGao setValuesForKeysWithDictionary:arr[i]];
            [self.dataArr addObject:gongGao];
        }
        if (self.params != parms) return ;
        [self.tableVie reloadData];
        [self.tableVie.mj_header endRefreshing];
        self.page = 1;
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        if (self.params != parms) return ;
        [[Toast shareInstance] makeToast:@"刷新失败,请检查网络" duration:2.0];
        [self.tableVie.mj_header endRefreshing];
    }];
}

-(void)loadMoreTopic
{
    [self.tableVie.mj_header endRefreshing];
    self.page++;
    //发送请求
    NSMutableDictionary *parms = [NSMutableDictionary dictionary];
    parms[@"page"] = @(self.page);
    parms[@"opt"] = @(1);
    self.params = parms;
    [[AFHTTPSessionManager manager] GET:GolobaUrl parameters:parms progress:nil success:^(NSURLSessionDataTask * _Nonnull task, NSDictionary* _Nullable responseObject) {
        if ([responseObject[@"error"] integerValue]!=-1) {
            [[Toast shareInstance] makeToast:@"网络加载失败,检查网络" duration:2.0];
            [self.tableVie.mj_footer endRefreshing];
            return ;
        }
        NSMutableArray *contentArr = responseObject[@"results"];
        if (contentArr.count<1) {
            [self.tableVie.mj_footer endRefreshingWithNoMoreData];
            return ;
        }
        for (int i = 0; i<contentArr.count; i++) {
            GongGaoModel *gongGao = [[GongGaoModel alloc] init];
            [gongGao setValuesForKeysWithDictionary:contentArr[i]];
            [self.dataArr addObject:gongGao];
        }
        [self.tableVie reloadData];
        [self.tableVie.mj_footer endRefreshing];
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        [[Toast shareInstance] makeToast:@"网络加载失败,检查网络" duration:2.0];
        if (self.params != parms) return ;
        [self.tableVie.mj_footer endRefreshing];
        //恢复页码,网络加载失败
        self.page--;
    }];
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
  GongGaoCell *cell =  [tableView dequeueReusableCellWithIdentifier:cellId forIndexPath:indexPath];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    [cell sendCellDataModel:self.dataArr[indexPath.row]];
    return cell;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.dataArr.count;
}

-(NSMutableArray *)dataArr
{
    if (!_dataArr) {
        _dataArr = [NSMutableArray array];
    }
    return _dataArr;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 80;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    GongGaoDetailVC *GongGaoDetail = [[GongGaoDetailVC alloc]init];
    GongGaoDetail.model = self.dataArr[indexPath.row];
    [self.navigationController pushViewController:GongGaoDetail animated:YES];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
